package com.utilities.fastaUtilities;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import com.utilities.parsers.ContigParser.ParseAllContigsFileParser;
import com.utilities.parsers.ContigParser.ParseEvents;
import com.utilities.parsers.ContigParser.ParseListener;

public class ExtractSubSequence extends ParseListener{
	
	private int startIndex = 1;
	private int endIndex = 1;
	private int currentStartIndex = 1;
	private int currentEndIndex = 0;
	private int readStatus = -1;
	private FileWriter fw = null;
	private StringBuffer strBuff = new StringBuffer();
	
	public static void main(String[] args) throws Exception
	{
		if (args.length == 0 )
		{
			args = new String[4];
			// input file
			args[0] = "C:/Users/bansalp/Desktop/DeNovo/7328Run1Run22/new/contig00030.txt";
			// start index
			args[1] = "16708";
			// end index
			args[2] = "17882";
			// output file
			args[3] = "C:/Users/bansalp/Desktop/DeNovo/7328Run1Run22/new/contigOut.txt";
		}
		
		ExtractSubSequence extractSubSequence = new ExtractSubSequence();
		extractSubSequence.startIndex = Integer.parseInt(args[1]);
		extractSubSequence.endIndex = Integer.parseInt(args[2]);
		extractSubSequence.fw = new FileWriter(new File(args[3]));
		ParseAllContigsFileParser allContigsFileParser = 
			new ParseAllContigsFileParser();
		allContigsFileParser.addListener(extractSubSequence);
		try 
		{
			allContigsFileParser.parse(new FileReader(args[0]));
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
	@Override
	public void handleEvent(ParseEvents event) 
	{
		try
		{
			switch((ParseEvents.EventTypes)event.getEventType())
			{
			case contigStarted:
			{
				fw.write(((String)event.getSource()).trim() + "\n");
				break;
			}
				
			case seqread : 
			{
				String str = ((String)event.getSource()).trim();
				currentEndIndex = currentStartIndex + str.length(); 
				String readString = "";
				int temp = 0;
				if (startIndex <= currentEndIndex)
				{
					if (readStatus == -1)
					{
						readStatus = 0;
						System.out.println("starting reading..");
						temp = startIndex - (currentStartIndex) ;
						readString = str.substring(temp);
					}
					else if (readStatus == 0)
					{
						readString = new String(str);
					}
				}
				if (endIndex < currentEndIndex)
				{
					if (readStatus == 0)
					{
						temp = endIndex - currentStartIndex - temp + 1;
						readStatus = 1;
						readString = readString.substring(0, temp);
					}
				}
				if (readStatus != -1)
				{
					write(readString);
					System.out.println(readString);
				}
				if (readStatus == 1)
				{
					if (strBuff.length() > 0)
					{
						fw.write(strBuff.toString());
					}
					fw.flush();
					fw.close();
					System.exit(0);
				}
				currentStartIndex = currentEndIndex;
			}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private void write(String readString) throws Exception 
	{
		strBuff.append(readString);
		if (strBuff.length() >= 60)
		{
			fw.write(strBuff.substring(0, 60) + "\n");
			strBuff.delete(0, 60);
		}
	}
}
